﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VietKinsNutrition.Bussiness;
using VietKinsNutrition.Common;
using System.IO;

namespace VietKinsNutrition.UserControl
{
    public partial class Thuvienmonan : DevExpress.XtraEditors.XtraUserControl
    {
        public Thuvienmonan()
        {
            InitializeComponent();
        }

        DataTable ReturnTbl;
        ClsTblDm_ThuvienmonanBLL ClsTblDm_Thuvienmonanbll = new ClsTblDm_ThuvienmonanBLL();
        int iBtn;
        DirectoryInfo df;
        private void Thuvienmonan_Load(object sender, EventArgs e)
        {
            this.ClsTblDm_Thuvienmonanbll.Bind_LoaimonIdCbo(cboLoaimon);
            this.ClsTblDm_Thuvienmonanbll.Bind_DotuoiIdCbo(cboThangtuoi);
            if (cboLoaimon.EditValue != null)
            {
                this.ClsTblDm_Thuvienmonanbll.BingSource_DataGrid(int.Parse(UserComboBoxItem.GetItemValue(cboLoaimon)), int.Parse(UserComboBoxItem.GetItemValue(cboThangtuoi)), this.gridControl1);
            }
            else

                this.ClsTblDm_Thuvienmonanbll.BingSource_DataGrid(this.gridControl1);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (this.gridView1.DataRowCount == 0)
                return;

            {
                if (XtraMessageBox.Show("Bạn muốn chắc chắn xóa dữ liệu?", "Chú ý", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try
                    {
                        this.ClsTblDm_Thuvienmonanbll.Id = int.Parse(this.gridView1.GetFocusedRowCellValue("Id").ToString());
                        this.ClsTblDm_Thuvienmonanbll.Del();
                        XtraMessageBox.Show("Xóa dữ liệu thành công");
                        this.ClsTblDm_Thuvienmonanbll.BingSource_DataGrid(int.Parse(UserComboBoxItem.GetItemValue(cboLoaimon)), int.Parse(UserComboBoxItem.GetItemValue(cboThangtuoi)), this.gridControl1);

                    }
                    catch
                    {
                        MessageBox.Show("Xóa thất bại");
                    }
                }
            }
            df = new DirectoryInfo(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\images\");
            //Tenfile = "";
            pictureEdit1.Refresh();
            pictureEdit1.Image = null;
            iBtn = 1;
            this.ClsTblDm_Thuvienmonanbll.ClearControl(txtTenmonan, cboLoaimon, txtThuongthuc, txtNguyenieu, pictureEdit1, txtCachthuchien, txtChuthich, txtNguontrich);
        }

        private void cboLoaimon_SelectedValueChanged(object sender, EventArgs e)
        {
            SelectValueChanged();
        }

        private void cboThangtuoi_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cboThangtuoi.EditValue.ToString().Trim() == "-Dùng chung-")
            {
                lbTuoi.Text = "(Dùng chung)";
            }
            string[] Par = cboThangtuoi.EditValue.ToString().Trim().Split('-');
            if (Par.Length > 1 && Par.Length < 3)
            {

                string k = System.Math.Round(((float.Parse(Par[0].ToString())) / 12), 1).ToString();
                string k1 = System.Math.Round(((float.Parse(Par[1].ToString())) / 12), 1).ToString();
                lbTuoi.Text = "(" + k + "-" + k1 + " tuổi)";
            }
            SelectValueChanged();
        }

        public void SelectValueChanged()
        {
            if (this.cboLoaimon.EditValue != null)
            {
                if (this.cboLoaimon.EditValue != null && this.cboThangtuoi.EditValue != null)
                {
                    this.ClsTblDm_Thuvienmonanbll.BingSource_DataGrid(int.Parse(UserComboBoxItem.GetItemValue(this.cboLoaimon)), int.Parse(UserComboBoxItem.GetItemValue(this.cboThangtuoi)), this.gridControl1);
                }
                else
                {
                    this.ClsTblDm_Thuvienmonanbll.ClearControl(txtTenmonan, cboLoaimon, txtThuongthuc, txtNguyenieu, pictureEdit1, txtCachthuchien, txtChuthich, txtNguontrich);
                }
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            string fPath = null;
            if (this.gridView1.GetFocusedRowCellValue("Hinhanh").ToString().Trim() != "")
            {
                fPath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\images\" + this.gridView1.GetFocusedRowCellValue("Hinhanh").ToString();
            }
            ClsExport2ExcelBLL ClsExport2Excelbll = new ClsExport2ExcelBLL();
            ClsExport2Excelbll.ExportFileWord(txtTenmonan.Text, cboLoaimon.EditValue.ToString(), cboThangtuoi.EditValue.ToString(), txtThuongthuc.Text, txtNguyenieu.Text, txtCachthuchien.Text, txtChuthich.Text, txtNguontrich.Text, fPath, gridView1);
        }
        string Tenfile = "";
        private void btnCapnhat_Click(object sender, EventArgs e)
        {
            if (ClsTblDm_Thuvienmonanbll.CheckNullTextbox(this.txtTenmonan) == false)
            {
                txtTenmonan.Focus();
                return;
            }
            if (ClsTblDm_Thuvienmonanbll.CheckNullTextbox(this.txtNguyenieu) == false)
            {
                txtNguyenieu.Focus();
                return;
            }
            if (ClsTblDm_Thuvienmonanbll.CheckNullTextbox(this.txtCachthuchien) == false)
            {
                txtCachthuchien.Focus();
                return;
            }
            if (cboLoaimon.EditValue == null)
            {
                cboLoaimon.Focus();
                return;
            }
            if (cboThangtuoi.EditValue == null)
            {
                cboThangtuoi.Focus();
                return;
            }
            switch (iBtn)
            {

                case 1:
                    {

                        ClsTblMonan_ThucphamBLL ClsTblMonan_Thucphambll = new ClsTblMonan_ThucphamBLL(ReturnTbl);
                        if (ClsTblDm_Thuvienmonanbll.FindSameName(iBtn, int.Parse(UserComboBoxItem.GetItemValue(cboLoaimon)), int.Parse(UserComboBoxItem.GetItemValue(cboThangtuoi)), this.txtTenmonan.Text) == false)
                        {
                            return;
                        }
                        try
                        {
                            Image pic;
                            pic = pictureEdit1.Image;
                            saveFileDialog1.FileName = openFileDialog1.FileName;
                            string[] parts = saveFileDialog1.FileName.Split('\\');
                            if (Tenfile == "")
                            {
                                Tenfile = parts[parts.Length - 1];
                            }
                            if (Tenfile != "")
                            {
                                pic.Save(df.FullName + Tenfile);
                                pictureEdit1.Image = Image.FromFile(df.FullName + Tenfile);
                            }
                        }
                        catch (Exception ex)
                        {
                            //MessageBox.Show("Không lưu được ảnh");
                            //return;
                        }
                        if (XtraMessageBox.Show("Bạn muốn thêm mới?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                            return;
                        this.ClsTblDm_Thuvienmonanbll.Insert(txtTenmonan.Text, int.Parse(UserComboBoxItem.GetItemValue(cboLoaimon)), int.Parse(UserComboBoxItem.GetItemValue(cboThangtuoi)), txtThuongthuc.Text, txtNguyenieu.Text, txtCachthuchien.Text, Tenfile, txtChuthich.Text, txtNguontrich.Text);
                        // GetByTopN_Id
                        DataTable GetopID = ClsTblDm_Thuvienmonanbll.GetByTopN_Id().Tables[0];
                        break;
                    }
                case 2:
                    {
                        ClsTblMonan_ThucphamBLL ClsTblMonan_Thucphambll = new ClsTblMonan_ThucphamBLL(ReturnTbl);
                        if (ClsTblDm_Thuvienmonanbll.FindSameName(iBtn, int.Parse(UserComboBoxItem.GetItemValue(cboLoaimon)), int.Parse(UserComboBoxItem.GetItemValue(cboThangtuoi)), this.txtTenmonan.Text) == false)
                        {
                            return;
                        }
                        try
                        {
                            Image pic;
                            pic = pictureEdit1.Image;
                            saveFileDialog1.FileName = openFileDialog1.FileName;
                            string[] parts = saveFileDialog1.FileName.Split('\\');
                            // Tenfile = parts[parts.Length - 1];
                            if (Tenfile != "")
                            {
                                Tenfile = DateTime.Now.ToFileTime().ToString().Substring(0, 6) + "_" + Tenfile;
                                pic.Save(df.FullName + Tenfile);
                                pictureEdit1.Image = Image.FromFile(df.FullName + Tenfile);
                            }
                        }
                        catch (Exception ex)
                        {
                            //  MessageBox.Show("Không lưu được ảnh");
                            //return;
                        }
                        if (XtraMessageBox.Show("Bạn muốn sửa dữ liệu?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                            Tenfile = "";
                            return;
                        this.ClsTblDm_Thuvienmonanbll.Edit(int.Parse(ClsTblDm_Thuvienmonanbll.Id.ToString()), txtTenmonan.Text, int.Parse(UserComboBoxItem.GetItemValue(cboThangtuoi)), int.Parse(UserComboBoxItem.GetItemValue(cboLoaimon)), txtThuongthuc.Text, txtNguyenieu.Text, txtCachthuchien.Text, Tenfile, txtChuthich.Text, txtNguontrich.Text);
                        Tenfile = "";
                        break;
                    }
            }
            this.ClsTblDm_Thuvienmonanbll.BingSource_DataGrid(int.Parse(UserComboBoxItem.GetItemValue(this.cboLoaimon)), int.Parse(UserComboBoxItem.GetItemValue(this.cboThangtuoi)), this.gridControl1);
            
            df = new DirectoryInfo(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\images\");
            Tenfile = "";
            pictureEdit1.Refresh();
            pictureEdit1.Image = null;
            iBtn = 1;
            this.ClsTblDm_Thuvienmonanbll.ClearControl(txtTenmonan, cboLoaimon, txtThuongthuc, txtNguyenieu, pictureEdit1, txtCachthuchien, txtChuthich, txtNguontrich);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            iBtn = 2;
            this.ClsTblDm_Thuvienmonanbll.BingSource_InputControl(txtTenmonan, cboLoaimon, cboThangtuoi, txtThuongthuc, txtNguyenieu, pictureEdit1, txtCachthuchien, txtChuthich, txtNguontrich, this.gridView1);
            simpleButton2.Enabled = true;
        }

        private void gridView1_Click(object sender, EventArgs e)
        {
            iBtn = 1;
            this.ClsTblDm_Thuvienmonanbll.ClearControl(txtTenmonan, cboLoaimon, txtThuongthuc, txtNguyenieu, pictureEdit1, txtCachthuchien, txtChuthich, txtNguontrich);
        }
        string image_outputDir;
        private void pictureEdit1_Click(object sender, EventArgs e)
        {
            if (iBtn == 1 || iBtn == 2)
            {
                image_outputDir = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
                df = new DirectoryInfo(image_outputDir + @"\images\");
                openFileDialog1.Filter = saveFileDialog1.Filter = "Image Files (JPEG,GIF,BMP)|*.jpg;*.jpeg;*.gif;*.bmp|JPEG Files(*.jpg;*.jpeg)|*.jpg;*.jpeg|GIF Files(*.gif)|*.gif|BMP Files(*.bmp)|*.bmp";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    String myFileName = openFileDialog1.FileName;
                    if (myFileName.Length != 0)
                    {
                        try
                        {
                            pictureEdit1.Image = Image.FromFile(myFileName);
                            string[] parts = myFileName.Split('\\');
                            Tenfile = parts[parts.Length - 1];
                            if (df.Exists)
                            {
                            }
                            else
                            {
                                df = Directory.CreateDirectory(image_outputDir + @"\images\");
                            }

                        }
                        catch
                        {
                            XtraMessageBox.Show(String.Format("Định dạng file không đúng", myFileName), "Thông báo", MessageBoxButtons.OK | MessageBoxButtons.RetryCancel);
                        }
                    }
                }
            }
        }
    }
}
